package tpi.gui.panels.info

import dominio.datos.Hobbie
import dominio.individuos.Villano
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.TextField
import org.apache.wicket.markup.html.panel.Panel
import org.apache.wicket.model.CompoundPropertyModel
import org.apache.wicket.model.Model
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XButton
import org.uqbar.wicket.xtend.XListView

abstract class HobbiesPanel extends Panel {
	
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	private final Villano villano
	private final Boolean conEdicion
	
	new(String id, Villano villano, Boolean conEdicion) {
		super(id)
		this.villano = villano
		this.conEdicion = conEdicion
		this.defaultModel = villano.asCompoundModel
		
		val Form<Villano> form = new Form<Villano>("hobbiesForm", new CompoundPropertyModel(villano))
		val listView = new XListView("hobbies")
		listView.populateItem = [c |
			c.model = c.modelObject.asCompoundModel
			c.addChild(new Label("dato"))
			if (conEdicion) c.addChild(new XButton("eliminar").onClick = [| eliminar(c.modelObject)])
		]
		form.addChild(listView)
		if (conEdicion) {
			val c = new TextField<String>("dato", new Model<String>)
			form.addChild(c)
			form.addChild(new XButton("agregarHobbie").onClick = [| 
				val dato = c.modelObject
				agregarHobbie(dato)
			])
		}
		addChild(form)
	}
	
	def agregarHobbie(String dato) {
		val hobbie = new Hobbie(dato)
		hobbie.validar()
		this.villano.hobbies += hobbie
			
	}
	
	def eliminar(Hobbie hobbie) {
		this.villano.hobbies.remove(hobbie)
	}
	
	
	
}